﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;

namespace SalienceEngineDemo
{
    public class Configuration
    {
                // Key value is the property name for the option changed, Value is the previous value of the option
        public SortedList<string, string> changedOptions;
        public List<Settings> userSettings;
        public String sInstallRoot;
        public int nNonCustomProfiles = 0;

        private string getInstallRoot()
        {
            RegistryKey key;
            string sRet = "";
            try
            {
                key = Registry.LocalMachine.OpenSubKey("Software\\Lexalytics");
                if (key != null)
                {
                #if DEBUG
                                    sRet = key.GetValue("DebugInstallRoot").ToString();
                #else
                                    sRet = key.GetValue("InstallRoot").ToString();
                #endif
                                }
                            }

            catch (Exception e)
            {
            }


            return sRet;
        }

        public Configuration()
        {
            sInstallRoot = getInstallRoot();
            this.changedOptions = new SortedList<string, string>();
            this.userSettings = new List<Settings>();
            if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\"))
            {
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\");
            }
            List<string> vAvoidRepeatDataSettings = new List<string>();
            int nAddedDefaultSettings = 0;
            foreach (string sDataDir in this.FindDataDirectories())
            {
                string sDataName = new DirectoryInfo(sDataDir).Name;
                string sSettingsPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\" + sDataName + "settings.bin";
                if (!File.Exists(sSettingsPath))
                {
                    if (sDataName.Equals("data"))
                    {
                        this.userSettings.Add(new Settings(sDataName, sDataDir, sInstallRoot, false));
                        this.userSettings.Add(new Settings("twitter_data", sDataDir, sInstallRoot, true));
                        vAvoidRepeatDataSettings.Add(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\twitter_datasettings.bin");
                        nAddedDefaultSettings++;
                        this.userSettings[this.nNonCustomProfiles].Save();
                        this.nNonCustomProfiles++;
                    }
                    else
                    {
                        this.userSettings.Add(new Settings(sDataName, sDataDir, sInstallRoot, false));
                    }
                    nAddedDefaultSettings++;
                }
                else
                {
                    this.userSettings.Add(new SettingsSerializer().Deserialize(sSettingsPath));
                    if (sDataName.Equals("data"))
                    {
                        this.userSettings.Add(new SettingsSerializer().Deserialize(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\twitter_datasettings.bin"));
                        vAvoidRepeatDataSettings.Add(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\twitter_datasettings.bin");
                        this.userSettings[this.nNonCustomProfiles].Save();
                        this.nNonCustomProfiles++;
                    }
                }
                this.userSettings[this.nNonCustomProfiles].Save();
                this.nNonCustomProfiles++;
                vAvoidRepeatDataSettings.Add(sSettingsPath);
            }
            bool bAddedCustom = false;
            foreach (string sDataDir in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Lexalytics\\Salience Demo\\"))
            {
                if (!vAvoidRepeatDataSettings.Contains(sDataDir))
                {
                    bAddedCustom = true;
                    this.userSettings.Add(new SettingsSerializer().Deserialize(sDataDir));
                }
            }
            Properties.Settings.Default.nMode = !bAddedCustom && nAddedDefaultSettings == this.nNonCustomProfiles ? 0 : Properties.Settings.Default.nMode + nAddedDefaultSettings;
        }

        public Settings activeSettings()
        {
            return this.userSettings[Properties.Settings.Default.nMode];
        }

        public void RestoreDefaults()
        {
            int currentSetting = Properties.Settings.Default.nMode;
            bool bShortMode = this.userSettings[currentSetting].settings["Name"].Equals("twitter_data");
            this.userSettings[currentSetting] = new Settings(this.userSettings[currentSetting].settings["Name"], this.userSettings[currentSetting].settingsPath, sInstallRoot, bShortMode);
            this.userSettings[currentSetting].Save();
        }

        private List<string> FindDataDirectories()
        {
            DirectoryInfo Lexalytics_Home = new DirectoryInfo(this.sInstallRoot);
            List<string> vRet = new List<string>();
            string[] DefaultDataNames = { "data", "zh-data", "es-data", "pt-data", "de-data", "fr-data" };
            foreach (DirectoryInfo folder in Lexalytics_Home.GetDirectories())
            {
                if (Array.Exists(DefaultDataNames, element => element == folder.Name))
                {
                    vRet.Add(folder.FullName);
                }
            }
            return vRet;
        }

        public bool UserProfileExists(string DataOrName)
        {
            for (int i = 0; i < this.userSettings.Count; i++)
            {
                if (this.userSettings[i].settings["Name"].Equals(DataOrName))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
